package cn.szzsi.common.kit;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class SDKHttpClient {

	// 下发
	public static String sendSMS(String url, String param) {
		String ret = "";
		url = url + "?" + param;
		String responseString = HttpClientUtil.getInstance().doGetRequest(url);
		responseString = responseString.trim();
		if (null != responseString && !"".equals(responseString)) {
			ret = xmlMt(responseString);
		}
		return ret;
	}
	// 统一解析格式
	public static String xmlResponse(String response) {
		String result = "失败";
		Document document = null;
		try {
			document = DocumentHelper.parseText(response);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		Element root = document.getRootElement();
		result = root.elementText("message");
		return result;
	}
	// 解析状态、上行
	private static List<Element> xmlDoc(String response) {
		boolean result = false;
		Document document = null;
		try {
			document = DocumentHelper.parseText(response);
		} catch (DocumentException e) {
			e.printStackTrace();
			return null;
		}
		Element root = document.getRootElement();
		List<Element> list = root.elements();
		List<Element> elemets = new ArrayList();
		// 增强for循环来遍历所有的U8ArrivalVouch节点
		for (Element element : list) {
			String message = element.getName();
			if ("message".equalsIgnoreCase(message)) {
				if (element.elements().size() > 0) {
					// System.out.println("--------------------"+element.elements().size());
					elemets.add(element);
				}
			}

		}
		return elemets;
	}

	// 解析下发response
	public static String xmlMt(String response) {
		String result = "0";
		Document document = null;
		try {
			document = DocumentHelper.parseText(response);
		} catch (DocumentException e) {
			e.printStackTrace();
			result = "-250";
		}
		Element root = document.getRootElement();
		result = root.elementText("error");
		if (null == result || "".equals(result)) {
			result = "-250";
		}
		return result;
	}
}
